package com.start.workflow.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.core.annotations.RegisterPermission;
import com.start.workflow.core.manage.StartActModelManage;
import com.start.workflow.model.StartProcessModel;
import com.start.workflow.model.StartProcessModelConfigDetail;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@Tag(name = "流程模型管理")
@RestController
@RequestMapping("/start/actModel")
public class StartActModelController {

    @Autowired
    private StartActModelManage startActModelManage;

    @GetMapping("/page")
    @PreAuthorize("@pas.hasPermission('start:act:model:info')")
    @RegisterPermission(value = "start:act:model:info", name = "查询流程模型")
    public Result<?> page(Page<StartProcessModel> page) {
        QueryWrapper<StartProcessModel> entity = DataQueryConstructor.createEntity(StartProcessModel.class);
        return Result.ok(startActModelManage.pageModel(page, entity));
    }

   @Operation(summary = "添加流程模型")
    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('start:act:model:add')")
    @RegisterPermission(value = "start:act:model:add", name = "添加流程模型")
    public Result<?> save(@RequestBody StartProcessModelConfigDetail modelVo) {
        startActModelManage.saveModel(modelVo);
        return Result.ok();
    }

   @Operation(summary = "流程模型信息")
    @GetMapping("/getActModelInfo/{modeId}")
    public Result<?> getActModelInfo(@PathVariable("modeId") String modeId) {
        return Result.ok(startActModelManage.getActModelInfo(modeId));
    }

   @Operation(summary = "获取模型配置详情")
    @PreAuthorize("@pas.hasPermission('start:act:model:info')")
    @RegisterPermission(value = "start:act:model:info", name = "查询流程模型")
    @GetMapping("/getActModelConfigDetail/{modeId}")
    public Result<?> getActModelConfigDetail(@PathVariable("modeId") String modeId) {
        return Result.ok(startActModelManage.getActModelConfigDetailByModel(modeId));
    }

   @Operation(summary = "修改模型")
    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('start:act:model:update')")
    @RegisterPermission(value = "start:act:model:update", name = "查询流程模型")
    public Result<?> updateById(@RequestBody StartProcessModelConfigDetail modelVo) {
        startActModelManage.updateModel(modelVo);
        return Result.ok();
    }

   @Operation(summary = "删除模型")
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('start:act:model:delete')")
    @RegisterPermission(value = "start:act:model:delete", name = "删除流程模型")
    public Result<?> deleteById(@PathVariable("id") String id) {
        startActModelManage.deleteModel(id);
        return Result.ok();
    }


   @Operation(summary = "部署模型")
    @GetMapping("/deploymentModel/{modelId}")
    @PreAuthorize("@pas.hasPermission('start:act:model:deployment')")
    @RegisterPermission(value = "start:act:model:deployment", name = "部署流程模型")
    public Result<?> deploymentModel(@PathVariable("modelId") String modelId) {
        startActModelManage.deploymentModel(modelId);
        return Result.ok();
    }

//   @Operation(summary = "分组获取模型")
//    @GetMapping("/getAllGroupModels")
//    @PreAuthorize("@pas.hasPermission('start:act:model:deployment')")
//    @RegisterPermission(value = "start:act:model:deployment", name = "部署流程模型")
//    public Result<?> getAllGroupModels() {
//        return Result.ok(startActModelManage.getGroupModels());
//    }


    @PreAuthorize("@pas.hasPermission('start:act:model:deployHistory')")
    @RegisterPermission(value = "start:act:model:deployHistory", name = "模型部署流程历史记录")
    @GetMapping("/getModelDeployHistory/{defKey}")
    public Result<?> getModelDeployHistory(@PathVariable("defKey") String defKey) {
        return Result.ok(startActModelManage.getModelDeployHistory(defKey));
    }

}
